/*
Copyright 2011 Raymond Giorgi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

package edu.pitt.cs3530.scripts;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * This class will analyze a trace file as outputed by the java scaling program
 * and report quality of service metrics
 */
public class AnalyzeQos {
	/**
	 * 
	 * @param args
	 *            The first argument is the trace file; The second argument is
	 *            the deadline; The third argument is the 'early' deadline; The
	 *            fourth argument is the relative time the program will start
	 *            looking at QoS metrics. If no time is provided, the program
	 *            will assume 0; The fifth argument is the relative time the
	 *            program will stop looking at QoS metrics. If no time is
	 *            provided, the program will use all remaining values
	 */
	public static void main(String args[]) {
		File file = new File(args[0]);
		long deadline = Long.parseLong(args[1]);
		long lowerDeadline = Long.parseLong(args[2]);
		long start = -1;
		if (args.length > 3) {
			start = Long.parseLong(args[3]);
		}
		long end = -1;
		if (args.length > 4) {
			end = Long.parseLong(args[4]);
		}
		List<File> fileList = new ArrayList<File>();
		if (file.isDirectory()) {
			for (File f : file.listFiles()) {
				fileList.add(f);
			}
		} else {
			fileList.add(file);
		}
		for (File f : fileList) {
			System.out.println(f.getName());
			printQos(f, deadline, lowerDeadline, start, end);
			System.out.println("");
		}
	}
	
	private static void printQos(File file, long deadline, long lowerDeadline, long start, long end) {
		int made = 0;
		int missed = 0;
		int lowerMade = 0;
		int total = 0;
		double totalQos = 0.0;
		double startTime = -1;
		try {
			BufferedReader reader = new BufferedReader(new FileReader(file));
			while (reader.ready()) {
				String fin = reader.readLine();
				if (fin.startsWith("fin")) {
					StringTokenizer tok = new StringTokenizer(fin, "-");
					tok.nextToken();
					double firstSec = Double.parseDouble(tok.nextToken());
					double firstUsec = Double.parseDouble(tok.nextToken());
					double first = firstSec * 1000000.0 + firstUsec / 1000.0;
					if (startTime == -1) {
						startTime = firstSec;
					}

					double secondSec = Double.parseDouble(tok.nextToken());
					double secondUsec = Double.parseDouble(tok.nextToken());
					double fina = ((secondSec - firstSec) * 1000000.0 + (secondUsec - firstUsec)) / 1000.0;
					// System.out.println(deadline + ":::" + fin + ":::" +
					// this.lowerDeadline);

					boolean okay = false;
					if (start != -1 && firstSec - startTime >= start) {
						okay = true;
						if (end != -1) {
							if (firstSec - startTime > end) {
								okay = false;
							}
						}
						if (okay) {
							if (deadline >= fina) {
								made++;
								if (deadline - fina <= lowerDeadline) {
									lowerMade++;
								}
							} else {
								missed++;
							}
							total++;
						}
					}
					totalQos += (fina) / deadline;
				}

			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Made: " + made);
		System.out.println("EarlyMade: " + lowerMade);
		System.out.println("Missed: " + missed);
		System.out.println("Total: " + (total / 5));
		System.out.println("Made Percent: " + ((double) made / (double) total));
		System.out.println("Missed Percent: "
				+ ((double) missed / (double) total));
		System.out.println("Qos: " + (totalQos / total));
	}
}
